import threading
import time

# 多线程安全问题 加锁
ticket = 100
# 创建锁
lock = threading.Lock()


def sale_ticket():
    global ticket
    for i in range(1000):
        lock.acquire()  # 上锁
        if ticket > 0:
            print(threading.current_thread().name + '>>正在出售第{}张票'.format(ticket))
            ticket -= 1
        time.sleep(1)
        lock.release()  # 释放锁


def start():
    for i in range(3):
        t = threading.Thread(target=sale_ticket)
        t.start()


if __name__ == '__main__':
    start()
